package com.opensource.svgaplayer;

import com.opensource.svgaplayer.drawer.SVGACanvasDrawer;
import com.opensource.svgaplayer.entities.SVGAAudioEntity;
import ohos.agp.components.Image;
import ohos.agp.render.Canvas;
import ohos.media.audio.SoundPlayer;
import java.util.List;
import static ohos.agp.components.Image.ScaleMode.STRETCH;

/**
 * ex.
 *
 * @author wl
 * @since 2021-06-28
 */
public class SVGADrawable {
    private SVGAVideoEntity videoItem;
    private SVGADynamicEntity dynamicItem;
    private Image.ScaleMode scaleType = STRETCH;
    private SVGACanvasDrawer drawer;
    private boolean cleared = true;
    private int currentFrame = 0;

    /**
     * SVGADrawable
     *
     * @param videoItem   SVGAVideoEntity
     * @param dynamicItem SVGADynamicEntity
     */
    public SVGADrawable(SVGAVideoEntity videoItem, SVGADynamicEntity dynamicItem) {
        this.videoItem = videoItem;
        this.dynamicItem = dynamicItem;
        drawer = new SVGACanvasDrawer(videoItem, dynamicItem);
    }

    /**
     * getVideoItem
     *
     * @return SVGAVideoEntity
     */
    public SVGAVideoEntity getVideoItem() {
        return videoItem;
    }

    /**
     * setVideoItem
     *
     * @param videoItem SVGAVideoEntity
     */
    public void setVideoItem(SVGAVideoEntity videoItem) {
        this.videoItem = videoItem;
    }

    /**
     * getDynamicItem
     *
     * @return SVGADynamicEntity
     */
    public SVGADynamicEntity getDynamicItem() {
        return dynamicItem;
    }

    /**
     * setDynamicItem
     *
     * @param dynamicItem SVGADynamicEntity
     */
    public void setDynamicItem(SVGADynamicEntity dynamicItem) {
        this.dynamicItem = dynamicItem;
    }

    /**
     * isCleared
     *
     * @return boolean
     */
    public boolean isCleared() {
        return cleared;
    }

    /**
     * setCleared
     *
     * @param cleared       boolean
     * @param svgaImageView SVGAImageView
     */
    public void setCleared(boolean cleared, SVGAImageView svgaImageView) {
        this.cleared = cleared;
        svgaImageView.invalidate();
    }

    /**
     * getScaleType
     *
     * @return Image.ScaleMode
     */
    public Image.ScaleMode getScaleType() {
        return scaleType;
    }

    /**
     * setScaleType
     *
     * @param scaleType Image.ScaleMode
     */
    public void setScaleType(Image.ScaleMode scaleType) {
        this.scaleType = scaleType;
    }

    /**
     * getCurrentFrame
     *
     * @return int
     */
    public int getCurrentFrame() {
        return currentFrame;
    }

    /**
     * setCurrentFrame
     *
     * @param currentFrame  int
     * @param svgaImageView SVGAImageView
     */
    public void setCurrentFrame(int currentFrame, SVGAImageView svgaImageView) {
        this.currentFrame = currentFrame;
        svgaImageView.invalidate();
    }

    /**
     * draw
     *
     * @param canvas Canvas
     * @param width  int
     * @param height int
     */
    public void draw(Canvas canvas, int width, int height) {
        if (cleared) {
            return;
        }
        if (canvas != null) {
            drawer.drawFrame(canvas, width, height, currentFrame, scaleType);
        }
    }

    /**
     * resume
     */
    public void resume() {
        List<SVGAAudioEntity> audio = videoItem.audioList;
        for (SVGAAudioEntity entity : audio) {
            videoItem.soundPlayer.resume(entity.getPlayID());
        }
    }

    /**
     * pause
     */
    public void pause() {
        List<SVGAAudioEntity> audio = videoItem.audioList;
        for (SVGAAudioEntity entity : audio) {
            videoItem.soundPlayer.pause(entity.getPlayID());
        }
    }

    /**
     * stop
     */
    public void stop() {
        List<SVGAAudioEntity> audio = videoItem.audioList;
        for (SVGAAudioEntity entity : audio) {
            SoundPlayer soundPool = videoItem.soundPlayer;
            if (soundPool != null && entity.getPlayID() != null) {
                soundPool.stop(entity.getPlayID());
            }
            soundPool.pauseAll();
        }
    }

    /**
     * clear
     */
    public void clear() {
        List<SVGAAudioEntity> audio = videoItem.audioList;
        for (SVGAAudioEntity entity : audio) {
            if (entity.getPlayID() != null) {
                videoItem.soundPlayer.stop(entity.getPlayID());
                entity.setPlayID(0);
            }
        }
        videoItem.clear();
    }
}
